import { Message } from 'iview';
import { findPermission, addPermission, delPermission, updatePermission, getAllModule } from '../../api/ServerApi';

const PermissionList = {
    namespaced: true,
    state() {
        return {
            queryParams: {
                pageNo: 1,
                pageSize: 10,
                module: null,
                title: null,
                permission: null,
                resourcesType: '2',
                createStart: null,
                createEnd: null
            },
            tableLoading: false,
            tableData: {
                list: [],
                total: 0,
                pageNum: 1,
                pageSize: 10
            },
            editModal: {
                show: false,
                loading: false
            },
            editParams: {
                oldPermission: null,
                module: null,
                title: null,
                permission: null,
                description: null
            },
            addModal: {
                show: false,
                loading: false
            },
            addParams: {
                module: null,
                title: null,
                permission: null,
                description: null
            },
            allModule: []
        };
    },
    mutations: {
        setTableLoading(state, tableLoading) {
            state.tableLoading = tableLoading;
        },
        setTableData(state, tableData) {
            state.tableData = tableData;
        },
        setEditModal(state, editModal) {
            state.editModal = { ...state.editModal, ...editModal };
        },
        setAddModal(state, addModal) {
            state.addModal = { ...state.addModal, ...addModal };
        },
        setAllModule(state, allModule) {
            state.allModule = allModule;
        }
    },
    actions: {
        queryData({ commit, state }, initPage) {
            if (initPage === true) {
                state.queryParams.pageNo = 1;
                state.queryParams.pageSize = 10;
            }
            commit('setTableLoading', true);
            findPermission(state.queryParams).then(data => {
                if (data.pageNum <= 0) {
                    data.pageNum = 1;
                }
                commit('setTableData', data);
                getAllModule().then(allModule => {
                    commit('setAllModule', allModule);
                    commit('setTableLoading', false);
                }).catch((error) => commit('setTableLoading', false));
            }).catch((error) => commit('setTableLoading', false));
        },
        deleteRow({ commit, dispatch }, { permission }) {
            commit('setTableLoading', true);
            delPermission(permission).then(data => {
                Message.success({ content: `删除成功 -> [${permission}]`, duration: 3 });
                dispatch('queryData');
            }).catch((error) => commit('setTableLoading', false));
        },
        updateRow({ commit, state, dispatch }) {
            commit('setEditModal', { loading: true });
            updatePermission(state.editParams.oldPermission, state.editParams).then(data => {
                commit('setEditModal', { show: false, loading: false });
                Message.success({ content: `更新成功 -> [${state.editParams.permission}]`, duration: 3 });
                dispatch('queryData');
            }).catch(error => commit('setEditModal', { loading: false }));
        },
        addRow({ commit, state, dispatch }) {
            commit('setAddModal', { loading: true });
            addPermission(state.addParams).then(data => {
                commit('setAddModal', { show: false, loading: false });
                Message.success({ content: `新增成功 -> [${state.addParams.permission}]`, duration: 3 });
                dispatch('queryData');
            }).catch(error => commit('setAddModal', { loading: false }));
        }
    }
};

export default PermissionList;
